package sis.ra.yahooNewsSummary;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import sis.ra.utility.Utils;
import sis.ra.yahoor.result.yahooresults;

public class extractYahooNewsFrmResults {

	public static ArrayList<yahooresults> getYahooResult(String yahooxml)
	{
		Document document = null;
		ArrayList<yahooresults> result=new ArrayList<yahooresults>();
		try {
			document = new SAXBuilder().build(new StringReader(yahooxml));
		} catch (Exception e) {
			System.out.println(yahooxml);
			e.printStackTrace();
		}
		Element root = document.getRootElement();
//		System.out.println(root);
		Namespace ns = Namespace.getNamespace("http://www.inktomi.com/");
//		List resultsets=root.getChildren();
//		for (int i=0;i<resultsets.size();i++)
//		{
//			System.out.println(resultsets.get(i));
//		}
		Element resultset=root.getChild("resultset_web", ns);
//		System.out.println(resultset);
		String hits=resultset.getAttributeValue("totalhits");
//		System.out.println(root+" "+resultset+" "+hits);
		List results=resultset.getChildren("result",ns);
//		System.out.println(results.size());
		for (int i=0;i<results.size();i++)
		{
			Element res=(Element)results.get(i);
			String abs=res.getChild("abstract",ns).getText();
			String title=res.getChild("title",ns).getText();
			String url=res.getChild("url",ns).getText();
			yahooresults yr=new yahooresults(abs,title,url);
			result.add(yr);
		}
		return result;
	}
	
	public static void main(String[] args) {
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo result_1003\\";
		File filepaths=new File (filepath);
		String[] files=filepaths.list();
		for (int i=0;i<files.length;i++)
		{
			String filename=filepath+files[i];
			if (!files[i].contains(".txt")) continue;
			String content=Utils.readFile(filename);
			ArrayList<yahooresults> results=getYahooResult(content);
			
			for (int j=0;j<results.size();j++)
			{	
				String url=results.get(j).getUrl();
				if (url.contains("news")&&url.contains("cnet"))
				System.out.println(url+"\t"+files[i]);
			}
		}
	}

}
